package com.tencreat.mall.controller.admin;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tencreat.common.annotation.Log;
import com.tencreat.common.core.HttpResult;
import com.tencreat.common.core.controller.BaseController;
import com.tencreat.common.core.domain.AjaxResult;
import com.tencreat.common.core.domain.model.ReqIdParam;
import com.tencreat.common.core.domain.model.ReqIdsParam;
import com.tencreat.common.enums.BusinessType;
import com.tencreat.common.utils.poi.ExcelUtil;
import com.tencreat.domain.account.MallUser;
import com.tencreat.domain.mall.MallAddress;
import com.tencreat.domain.mall.dto.MallAddressDTO;
import com.tencreat.domain.mall.vo.MallAddressVO;
import com.tencreat.service.account.IMallUserService;
import com.tencreat.service.mall.IMallAddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 收货地址Controller
 *
 * @author tencreat
 * @date 2021-08-15
 */
@Api(tags="收货地址api")
@RestController
@RequestMapping("/mall/address")
public class AdminMallAddressController extends BaseController {
    @Autowired
    private IMallAddressService mallAddressService;

    @Autowired
    private IMallUserService mallUserService;

    /**
     * 查询收货地址列表
     */
    @ApiOperation(value="查询收货地址列表")
    @PreAuthorize("@ss.hasPermi('mall:address:list')")
    @PostMapping("/list")
    public HttpResult<IPage<MallAddress>> list(@RequestBody  MallAddress mallAddress) {
        LambdaQueryWrapper<MallAddress> wrapper = new LambdaQueryWrapper<>();
        Page<MallAddress> page =  new Page<>(mallAddress.getPageNum(), mallAddress.getPageSize());
        if (mallAddress.getMobile() != null){
            List<MallUser> list = mallUserService.lambdaQuery().eq(MallUser::getMobile, mallAddress.getMobile()).list();
            if (list != null && list.size() >0){
                List<Long> uuids = list.stream().map(MallUser::getUuid).collect(Collectors.toList());
                wrapper.in(MallAddress::getUuid,uuids);
            } else {
                return HttpResult.ok(page);
            }
        }
        // 相关模糊搜索
        if (StrUtil.isNotEmpty(mallAddress.getTel())) {
            wrapper.eq(MallAddress::getTel, mallAddress.getTel());
        }
        if (StrUtil.isNotEmpty(mallAddress.getName())) {
            wrapper.eq(MallAddress::getName, mallAddress.getName());
        }
        if (mallAddress.getUuid() != null) {
            wrapper.eq(MallAddress::getUuid, mallAddress.getUuid());
        }
        if (mallAddress.getIsDefault() != null) {
            wrapper.eq(MallAddress::getIsDefault, mallAddress.getIsDefault());
        }
        if (mallAddress.getEndTime() != null) {
            wrapper.lt(MallAddress::getCreateTime, mallAddress.getEndTime());
        }
        if (mallAddress.getStartTime() != null) {
            wrapper.gt(MallAddress::getCreateTime, mallAddress.getStartTime());
        }
        wrapper.orderByDesc(MallAddress::getCreateTime);
        Page<MallAddress> addressPage = mallAddressService.page(page, wrapper);
        List<MallAddress> records = addressPage.getRecords();
        records.stream().peek(item -> {
            MallUser mallUser = mallUserService.lambdaQuery().eq(MallUser::getUuid, item.getUuid()).one();
            item.setMobile(mallUser == null ? "": mallUser.getMobile());
        }).collect(Collectors.toList());
        addressPage.setRecords(records);
        return HttpResult.ok(addressPage);
    }

    /**
     * 导出收货地址列表
     */
    @ApiOperation(value="导出收货地址列表")
    @PreAuthorize("@ss.hasPermi('mall:address:export')")
    @Log(title = "用户管理>用户地址", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public AjaxResult export(@RequestBody  MallAddress mallAddress) {
        LambdaQueryWrapper<MallAddress> wrapper = new LambdaQueryWrapper<>();
        Page<MallAddress> page =  new Page<>(1, Integer.MAX_VALUE);
        if (mallAddress.getMobile() != null){
            List<MallUser> list = mallUserService.lambdaQuery().eq(MallUser::getMobile, mallAddress.getMobile()).list();
            if (list != null && list.size() >0){
                List<Long> uuids = list.stream().map(MallUser::getUuid).collect(Collectors.toList());
                wrapper.in(MallAddress::getUuid,uuids);
            }else {
                List<MallAddress> list2 = new ArrayList<>();
                ExcelUtil<MallAddress> util = new ExcelUtil<MallAddress>(MallAddress.class);
                return util.exportExcel(list2, "收货地址列表");
            }
        }
        // 相关模糊搜索
        if (StrUtil.isNotEmpty(mallAddress.getTel())) {
            wrapper.eq(MallAddress::getTel, mallAddress.getTel());
        }
        if (StrUtil.isNotEmpty(mallAddress.getName())) {
            wrapper.eq(MallAddress::getName, mallAddress.getName());
        }
        if (mallAddress.getUuid() != null) {
            wrapper.eq(MallAddress::getUuid, mallAddress.getUuid());
        }
        if (mallAddress.getIsDefault() != null) {
            wrapper.eq(MallAddress::getIsDefault, mallAddress.getIsDefault());
        }
        if (mallAddress.getEndTime() != null) {
            wrapper.lt(MallAddress::getCreateTime, mallAddress.getEndTime());
        }
        if (mallAddress.getStartTime() != null) {
            wrapper.gt(MallAddress::getCreateTime, mallAddress.getStartTime());
        }
        wrapper.orderByDesc(MallAddress::getCreateTime);
        Page<MallAddress> addressPage = mallAddressService.page(page, wrapper);
        List<MallAddress> records = addressPage.getRecords();
        records.stream().peek(item -> {
            item.setAreasName(item.getProvinceName()+item.getCityName()+item.getCountyName());
            item.setCreateTime2(item.getCreateTime());
            MallUser mallUser = mallUserService.lambdaQuery().eq(MallUser::getUuid, item.getUuid()).one();
            item.setMobile(mallUser == null ? "": mallUser.getMobile());
        }).collect(Collectors.toList());
        addressPage.setRecords(records);
        List<MallAddress> list = addressPage.getRecords();
        ExcelUtil<MallAddress> util = new ExcelUtil<MallAddress>(MallAddress.class);
        return util.exportExcel(list, "收货地址列表");
    }

    /**
     * 获取收货地址详细信息
     */
    @ApiOperation(value="获取收货地址详细信息")
    @PreAuthorize("@ss.hasPermi('mall:address:query')")
    @PostMapping(value = "/getInfo")
    public HttpResult<MallAddress> getInfo(@RequestBody   ReqIdParam id) {
        return HttpResult.ok(mallAddressService.selectMallAddressById(id));
    }

    /**
     * 新增收货地址
     */
    @ApiOperation(value="新增收货地址")
    @PreAuthorize("@ss.hasPermi('mall:address:add')")
    @Log(title = "收货地址", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public HttpResult<Boolean> add(@RequestBody  MallAddress mallAddress) {
        return to(mallAddressService.insertMallAddress(mallAddress));
    }

    /**
     * 修改收货地址
     */
    @ApiOperation(value="修改收货地址")
    @PreAuthorize("@ss.hasPermi('mall:address:edit')")
    @Log(title = "收货地址", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/edit")
    public HttpResult<Boolean> edit(@RequestBody MallAddressDTO mallAddressDTO) {
        return to(mallAddressService.updateMallAddress(mallAddressDTO));
    }

    /**
     * 删除收货地址
     */
    @ApiOperation(value="删除收货地址")
    @PreAuthorize("@ss.hasPermi('mall:address:remove')")
    @Log(title = "收货地址", businessType = BusinessType.DELETE)
	@PostMapping("/remove")
    public HttpResult<Boolean> remove(@RequestBody  ReqIdsParam ids) {
        return to(mallAddressService.deleteMallAddressByIds(ids));
    }

}
